package com.ptswitch.material.model.common;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.validation.constraints.Size;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.validator.constraints.Email;

import com.ptswitch.material.model.security.Role;
import com.ptswitch.material.model.security.UserRole;
import com.ptswitch.material.util.security.RoleEditUtil;

@Entity
public class Uzer extends org.hxzon.project.model.Uzer {

    public static final int Department_None = 0;
    public static final int Department_Financial = 1;
    public static final int Department_Buyer = 2;
    public static final int Department_Production = 3;
    public static final int Department_Hardware = 4;
    public static final int Department_Software = 5;
    public static final int Department_ContractAuditor = 6;

    private static final long serialVersionUID = 7570429986649512051L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq_gen_user")
    @javax.persistence.SequenceGenerator(name = "id_seq_gen_user", sequenceName = "seq_id_user", allocationSize = 20)
    private long id;

    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
    @Cascade(value = { org.hibernate.annotations.CascadeType.DELETE, //
            org.hibernate.annotations.CascadeType.PERSIST })
    private List<UserRole> userRoles;

    @ColumnDefault(StringDefaultValue)
    @Size(max = DefineSize2048)
    // @NotBlank
    @Email
    private String email;

    @Column(length = 2)
    private int department = Department_None;

    //
    @Override
    public String toString() {
        return this.getRealname() + "(" + this.getUsername() + ")";
    }

    public void addRole(Role role) {
        if (userRoles == null) {
            userRoles = new ArrayList<UserRole>();
        }
        userRoles.add(new UserRole(this, role));
    }

    public List<Role> getRoles() {
        return RoleEditUtil.getRoles(this);
    }

    //===============
    public Uzer() {
    }

    public Uzer(String username, String password, String realname, int department) {
        setUsername(username);
        setPassword(password);
        setRealname(realname);
        setDepartment(department);
    }

    //
    public String getEmail() {
        return email;
    }

    public List<UserRole> getUserRoles() {
        return userRoles;
    }

    public void setUserRoles(List<UserRole> userRoles) {
        this.userRoles = userRoles;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getDepartment() {
        return department;
    }

    public void setDepartment(int department) {
        this.department = department;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
}
